import Vue from 'vue';
import Vuex from 'vuex';
import Storage from '@/utils/storage';
import { TOKEN_KEY } from '@/utils/consts';
import { userReq } from '@/api/user';
Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        userToken: Storage.getItemS(TOKEN_KEY),
        userInfo: null
    },
    mutations: {
        saveToken: (state, payload = null) => {
            state.userToken = payload;
            Storage.setItemS(TOKEN_KEY, payload);
            return 0;
        },
        setUserInfo: (state, payload = null) => {
            state.userInfo = payload;
            return 0;
        }
    },
    actions: {
        getUser: async context => {
            try {
                const { data: res } = await userReq();
                context.commit('setUserInfo', res.data);
            } catch (err) {
            }
        }
    },
    modules: {
    }
});
